package cn.shop.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.admin.service.FundDetailService;
import cn.shop.base.MyPage;
import cn.shop.transcation.detail.entity.FundDetail;
import cn.shop.transcation.detail.entity.WithdrawDetail;
import cn.shop.transcation.detail.mapper.FundDetailMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

/**
 * 交易明细(FundDetail)表服务实现类
 *
 * @author jzw
 * @since 2021-12-06 14:22:11
 */
@Service("fundDetailService")
public class FundDetailServiceImpl extends ServiceImpl<FundDetailMapper, FundDetail> implements FundDetailService {

	@Override
	public MyPage<WithdrawDetail> queryByPageForWithdraw(MyPage<FundDetail> page, QueryWrapper<FundDetail> query) {
		return baseMapper.queryByPageForWithdraw(page, query);
	}

	@Override
	public Map<String, BigDecimal> statistics(FundDetail detail, LocalDateTime startTime, LocalDateTime endTime) {
		if (detail.getType() == null) {
			return statisticsAll(detail, startTime, endTime);
		}
		LambdaQueryWrapper<FundDetail> query = Wrappers.<FundDetail>lambdaQuery()
				.eq(StrUtil.isNotBlank(detail.getGoods()), FundDetail::getGoods, detail.getGoods())
				.eq(detail.getOrderId() != null, FundDetail::getOrderId, detail.getOrderId())
				.eq(detail.getUid() != null, FundDetail::getUid, detail.getUid())
				.eq(detail.getPlayerId() != null, FundDetail::getPlayerId, detail.getPlayerId())
				.like(StrUtil.isNotBlank(detail.getPlayerAccount()), FundDetail::getPlayerAccount, detail.getPlayerAccount())
				.eq(detail.getType() != null, FundDetail::getType, detail.getType())
				.apply(startTime != null,
						"create_time >={0}", startTime)
				.apply(endTime != null,
						"create_time <= {0}", endTime);
		Map<String, BigDecimal> map = baseMapper.sumAmount(query);
		long count = count(query);
		map.put("count", new BigDecimal(count));
		return map;
	}

	@Override
	public Map<String, BigDecimal> statisticsForWithdraw(FundDetail detail, LocalDateTime startTime, LocalDateTime endTime) {

		QueryWrapper<FundDetail> query = Wrappers.<FundDetail>query()
				.eq(detail.getOrderId() != null, "fd.order_id", detail.getOrderId())
				.like(StrUtil.isNotBlank(detail.getPlayerAccount()), "fd.player_account", detail.getPlayerAccount())
				.eq("w.state", 3)
				.eq(detail.getType() != null, "fd.type", 3)
				.apply(startTime != null,
						"w.payment_time >={0}", startTime)
				.apply(endTime != null,
						"w.payment_time <= {0}", endTime);
		Map<String, BigDecimal> typeZero = baseMapper.sumAmountForWithdraw(query);
		long count = baseMapper.countForWithdraw(query);
		typeZero.put("count", new BigDecimal(count));
		return typeZero;
	}

	public Map<String, BigDecimal> statisticsAll(FundDetail detail, LocalDateTime startTime, LocalDateTime endTime) {
		long count = count(Wrappers.<FundDetail>lambdaQuery()
				.eq(StrUtil.isNotBlank(detail.getGoods()), FundDetail::getGoods, detail.getGoods())
				.eq(detail.getOrderId() != null, FundDetail::getOrderId, detail.getOrderId())
				.eq(detail.getUid() != null, FundDetail::getUid, detail.getUid())
				.eq(detail.getPlayerId() != null, FundDetail::getPlayerId, detail.getPlayerId())
				.like(StrUtil.isNotBlank(detail.getPlayerAccount()), FundDetail::getPlayerAccount, detail.getPlayerAccount())
				.in(FundDetail::getType, Arrays.asList(1, 2))
				.apply(startTime != null,
						"create_time >= {0}", startTime)
				.apply(endTime != null,
						"create_time <= {0}", endTime));
		Map<String, BigDecimal> typeZero = baseMapper.sumAmount(Wrappers.<FundDetail>lambdaQuery()
				.eq(StrUtil.isNotBlank(detail.getGoods()), FundDetail::getGoods, detail.getGoods())
				.eq(detail.getOrderId() != null, FundDetail::getOrderId, detail.getOrderId())
				.eq(detail.getUid() != null, FundDetail::getUid, detail.getUid())
				.eq(detail.getPlayerId() != null, FundDetail::getPlayerId, detail.getPlayerId())
				.like(StrUtil.isNotBlank(detail.getPlayerAccount()), FundDetail::getPlayerAccount, detail.getPlayerAccount())
				.eq(FundDetail::getType, 1)
				.apply(startTime != null,
						"create_time >={0}", startTime)
				.apply(endTime != null,
						"create_time <= {0}", endTime));
		Map<String, BigDecimal> typeTwo = baseMapper.sumAmount(Wrappers.<FundDetail>lambdaQuery()
				.eq(StrUtil.isNotBlank(detail.getGoods()), FundDetail::getGoods, detail.getGoods())
				.eq(detail.getOrderId() != null, FundDetail::getOrderId, detail.getOrderId())
				.eq(detail.getUid() != null, FundDetail::getUid, detail.getUid())
				.eq(detail.getPlayerId() != null, FundDetail::getPlayerId, detail.getPlayerId())
				.like(StrUtil.isNotBlank(detail.getPlayerAccount()), FundDetail::getPlayerAccount, detail.getPlayerAccount())
				.eq(FundDetail::getType, 2)
				.apply(startTime != null,
						"create_time >= {0}", startTime)
				.apply(endTime != null,
						"create_time <= {0}", endTime));
		BigDecimal amount = typeZero.get("amounts").add(ObjectUtil.defaultIfNull(typeTwo.get("amounts"), BigDecimal.ZERO));
		typeZero.put("amounts", amount);
		typeZero.put("count", new BigDecimal(count));
		return typeZero;
	}


}

